﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;

public partial class Charts_Bar_CenterStandardUsage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CenterStandardUsageChart.Titles[0].Text = string.Format("نمودار مقایسه مصرف انرژی {1} و استاندارد مرکز {0}", CenterDropDownList.SelectedItem, TypeDropDownList.SelectedItem);
    }
    protected void ChartLinkButton_Click(object sender, EventArgs e)
    {
        CenterStandardUsageSqlDataSource.DataBind();
        
        Series usageSeries = new Series();
        usageSeries.Name = "میزان مصرف";        

        DataView dataView = new DataView();
        DataTable dataTable = new DataTable();
        dataView = CenterStandardUsageSqlDataSource.Select(DataSourceSelectArguments.Empty) as DataView;
        byte color = 0;
        int usage = 0;
        int max = 0;
        int min = 0;

        if (dataView != null)
        {
            dataTable = dataView.ToTable();            
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {                
                usage = int.Parse(dataTable.Rows[i].ItemArray[0].ToString());
                min = int.Parse(dataTable.Rows[i].ItemArray[1].ToString());
                max = int.Parse(dataTable.Rows[i].ItemArray[2].ToString()); 

                usageSeries.Points.AddXY(dataTable.Rows[i].ItemArray[3], usage);
                
                if(usage < min)
                    color = 0;
                else if(usage > max)
                    color = 2;
                else
                    color = 1;

                if( color == 0)
                    ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(usageSeries.Points[i])).Color = Color.Yellow;
                else if (color == 1)
                    ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(usageSeries.Points[i])).Color = Color.Green;
                else if (color == 2)
                    ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(usageSeries.Points[i])).Color = Color.Red;
            }            
            CenterStandardUsageChart.Series.Add(usageSeries);

            CenterStandardUsageChart.DataBind();
        }
    }
}